﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace FuelDXApplication1
{
    public class DBHelper
    {
        static string urll = AppDomain.CurrentDomain.BaseDirectory;

        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            //ConnectionString = "Data Source=E:\\项目\\作业代码\\实训一\\项目二Fuel\\sqlites.db", // 注意路径中的反斜杠需要双写
            DbType = DbType.Sqlite,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute
        });

        /// <summary>
        /// 测试完已可用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// 插入方法
        public static async Task<int> Insert<T>(T entity) where T : class, new()
        {
            return await Db.Insertable(entity).ExecuteCommandAsync();
        }
        /// <summary>
        /// 批量插入方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static async Task<int> InsertRange<T>(List<T> entities) where T : class, new()
        {
            return await Db.Insertable(entities).ExecuteCommandAsync();
        }

        /// <summary>
        /// 测试完已可用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        // 删除方法
        public static async Task<int> Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
        {
            return await Db.Deleteable<T>(whereExpression).ExecuteCommandAsync();
        }

        /// <summary>
        /// 测试可用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <returns></returns>
        // 更新方法
        public static async Task<int> Update<T>(T entity) where T : class, new()
        {
            return await Db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 测试可用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        // 查询方法
        public static async Task<List<T>> Query<T>() where T : class, new()
        {
            return await Db.Queryable<T>().ToListAsync();
        }

        /// <summary>
        /// 查询方法，根据传入的条件进行查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async Task<List<T>> QueryC<T>(Expression<Func<T, bool>> condition) where T : class, new()
        {
            var queryable = Db.Queryable<T>();

            // 根据传入的条件表达式添加查询条件
            if (condition != null)
            {
                queryable = queryable.Where(condition);
            }

            return await queryable.ToListAsync();
        }


       

    }
}
